package com.example.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.example.entity.Project;
import com.example.mapper.KanbanMapper;
import com.example.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.entity.Group;
import com.example.mapper.GroupMapper;
import org.springframework.transaction.annotation.Transactional;

@RestController
@RequestMapping("/api/kanban")
public class KanbanController {

    @Autowired
    private KanbanMapper kanbanMapper;
    
    // 注入GroupMapper
    @Autowired
    private GroupMapper groupMapper;

    @GetMapping("/projects")
    public Result getProjects(String name, String status, String priority, String username) {
        try {
            List<Project> projects = kanbanMapper.getProjectsWithStatusAndProgress(name, status, priority, username);
            return Result.success(projects);
        } catch (Exception e) {
            return Result.error("获取项目列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/projects/{id}")
    public Result getProjectById(@PathVariable Integer id) {
        try {
            if (id == null || id <= 0) {
                return Result.error("项目ID不能为空或无效");
            }
            Project project = kanbanMapper.getProjectById(id);
            if (project != null) {
                return Result.success(project);
            } else {
                return Result.error("项目不存在");
            }
        } catch (Exception e) {
            return Result.error("获取项目详情失败: " + e.getMessage());
        }
    }

    @PostMapping("/projects")
    @Transactional // 添加事务管理
    public Map<String, Object> createProject(@RequestBody Project project) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 参数验证
            if (project == null) {
Map<String, Object> errorResult = new HashMap<>();
errorResult.put("code", Result.error("项目信息不能为空").getCode());
errorResult.put("msg", Result.error("项目信息不能为空").getMsg());
return errorResult;
            }
            if (project.getProjectName() == null || project.getProjectName().trim().isEmpty()) {
Map<String, Object> errorResult = new HashMap<>();
errorResult.put("code", Result.error("项目名称不能为空").getCode());
errorResult.put("msg", Result.error("项目名称不能为空").getMsg());
return errorResult;
            }
            // 添加时间验证
            if (project.getStartTime() != null && project.getEndTime() != null) {
                if (project.getStartTime().after(project.getEndTime())) {
Map<String, Object> errorResult = new HashMap<>();
errorResult.put("code", Result.error("结束时间不能早于开始时间").getCode());
errorResult.put("msg", Result.error("结束时间不能早于开始时间").getMsg());
return errorResult;
                }
            }
            
            // 插入项目记录
            kanbanMapper.insertProject(project);
            
            // 不再自动创建小组，小组管理功能集中到小组管理界面
            
            result.put("code", "200");
            result.put("msg", "项目创建成功");
        } catch (Exception e) {
Map<String, Object> errorResult = new HashMap<>();
Result error = Result.error("项目创建失败: " + e.getMessage());
errorResult.put("code", error.getCode());
errorResult.put("msg", error.getMsg());
return errorResult;
        }
        return result;
    }

    @PutMapping("/projects/{id}")
    public Result updateProject(@PathVariable Integer id, @RequestBody Project project) {
        try {
            // 参数验证
            if (id == null || id <= 0) {
                return Result.error("项目ID不能为空或无效");
            }
            if (project == null) {
                return Result.error("项目信息不能为空");
            }
            // 添加时间验证
            if (project.getStartTime() != null && project.getEndTime() != null) {
                if (project.getStartTime().after(project.getEndTime())) {
                    return Result.error("结束时间不能早于开始时间");
                }
            }
            project.setProjectId(id);
            kanbanMapper.updateProject(project);
            return Result.success("项目更新成功");
        } catch (Exception e) {
            return Result.error("项目更新失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/projects/{id}")
    public Result deleteProject(@PathVariable Integer id) {
        try {
            if (id == null || id <= 0) {
                return Result.error("项目ID不能为空或无效");
            }
            kanbanMapper.deleteProject(id);
            return Result.success("项目删除成功");
        } catch (Exception e) {
            return Result.error("项目删除失败: " + e.getMessage());
        }
    }
}